const app = getApp();

Page({
    data: {
        addFlag: true,
        positionToShow: []
        // positionToShow: [
        //     {
        //         posititon: "西安电子科技大学",
        //         longitude: 108.840053,
        //         latitude: 34.129522,
        //     },
        //     {
        //         posititon: "西安钟楼",
        //         longitude: 108.95346,
        //         latitude: 34.265725,
        //     },
        //     {
        //         posititon: "西安大雁塔北广场",
        //         longitude: 108.971152,
        //         latitude: 34.225619,
        //     },
        //     {
        //         posititon: "大唐不夜城",
        //         longitude: 108.970556,
        //         latitude: 34.220781,
        //     },
        //     {
        //         posititon: "",
        //         longitude: 108.970556,
        //         latitude: 34.220781,
        //     },
        // ],
    },
    onLoad() {
        this.getMyData()
    },
    getMyData() {
        let _positionToShow = []
        let _activities = app.globalData.activities
        _activities.forEach(item => {
            let _positionItem = {}
            _positionItem.position = item.positions[0].position_name
            _positionItem.longitude = item.positions[0].longitude
            _positionItem.latitude = item.positions[0].latitude
            _positionToShow.push(_positionItem)
        });
        this.setData({
            positionToShow: _positionToShow
        })
        // console.log(this.data.positionToShow);

    },
    getLocation() {
        swan.getLocation({
            type: 'gcj02',
            altitude: true,
            success: res => {
                console.log('getLocation success', res);
                let _latitude = res.latitude
                let _longitude = res.longitude
                // let _latitude = this.formatLocation(res.latitude) + '′'
                // let _longitude = this.formatLocation(res.longitude) + '′'
                this.setData({
                    latitude: _latitude,
                    longitude: _longitude,
                });
                // console.log(this.getData());

                // swan.openLocation({
                //     latitude: res.latitude,
                //     longitude: res.longitude,
                //     success: res => {
                //         console.log('openLocation success', res);
                //     },
                //     fail : function (err) {
                //         console.log('openLocation fail', err);
                //     }
                // });
            },
            fail: err => {
                swan.showToast({title: '获取失败'});
            }
        });
    },
    formatLocation(data) {
        return data.toFixed(2).replace('.', '°');
    },
    remove(e) {
        let index = e.currentTarget.dataset.index
        let _positionToShow = this.data.positionToShow
        _positionToShow.splice(index, 1)
        this.setData({
            positionToShow: _positionToShow
        })
        // console.log(this.data.positionToShow);
    },
    add() {
        if(this.data.addFlag) {
            // console.log('增加地点');
            let newPosition = {
                posititon: "",
                longitude: 108.970556,
                latitude: 34.220781,
            }
            let _positionToShow = this.data.positionToShow
            _positionToShow.push(newPosition)
            this.setData({
                positionToShow: _positionToShow
            })

        } else {
            // console.log('请在在输入框中增加内容');
        }
    },
    isDisable() {

    },
    complete() {
        // console.log('完成');
        // swan.navigateTo({
        //     url: "../../profile_pages/participation/participation"
        // })
        swan.navigateBack({})
    }

});

